﻿using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;
using WpfTwitterClient.Infrastructure.Models;

namespace WpfTwitterClient.Modules.Timelines.Views.HomeTimeline
{
    public class HomeTimelineModel : DispatcherObject, IHomeTimelineModel, INotifyPropertyChanged
    {
        private Tweets _tweets;
        private Dispatcher dispatcherUIThread;
        private delegate void SetTweetsDelegate(Tweets tweets);

        public HomeTimelineModel(Dispatcher dispatcherUIThread)
        {
            this.dispatcherUIThread = dispatcherUIThread;
        }

        public Tweets Tweets { get { return _tweets; } }

        public void SetTweets(Tweets tweets)
        {
                if (dispatcherUIThread.CheckAccess())
                {
                    _tweets = tweets;
                    OnPropertyChanged("Tweets");
                } else
                {
                    dispatcherUIThread.BeginInvoke(DispatcherPriority.Send, new SetTweetsDelegate(SetTweets),
                                                   new object[] {tweets});
                }
        }


        public static string HeaderInfo
        {
            get { return "Friends"; }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if(PropertyChanged == null)
                return;

            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
